<?php

class SoutezModel extends NObject
{
	private $table = 'dbo.Soutez';
	private $tym = 'dbo.Tym_Soutez';
	private $rozhodci = 'dbo.Rozhodci_Soutez';
	private $pozice = 'dbo.Pozice_Soutez';
	private $connection;


	public static function initialize()
	{
		dibi::connect(NEnvironment::getConfig('database'));
	}
	
	public function __construct()
	{
		$this->connection = dibi::getConnection();
	}
	//-----------
	//FIND ALL
	//-----------	
	public function findAll()
	{
		return $this->connection->select('*')->from($this->table);
	}
	//-----------
	//FIND BY ID
	//-----------
	public function findById($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->table." WHERE id=%i",$id);
		//return $this->connection->select('*')->from($this->table)->where('id=%i',$id);
	}
	//-----------
	//FIND BY LIGA_ID
	//-----------
	public function findByLigaId($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->table." WHERE liga_id=%i",$id);
	}
	//-----------
	//FIND BY LIGA_ID
	//-----------
	public function findBySezonaId($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->table." WHERE sezona_id=%i",$id);
	}
	//-----------
	//UPDATE
	//-----------
	public function update($id, array $data)
	{
		return $this->connection->update($this->table, $data)->where('id=%i', $id)->execute();
	}
	//-----------
	//INSERT
	//-----------	
	public function insert(array $data)
	{
		return $this->connection->insert($this->table, $data)->execute(dibi::IDENTIFIER);
	}
	//-----------
	//FIND ALL
	//-----------
	public function delete($id)
	{
		return $this->connection->query("DELETE FROM ".$this->table." WHERE id=%i", $id);
		//return $this->connection->delete($this->table)->where('id=%i', $id)->execute();
	}
	
	//RELACE s tabulkou TYM_SOUTEZ
	public function tym_soutez_insert2($tym_ids, $soutez_id)
	{
		$data = array();
		foreach($tym_ids as $id){
			$data= array(
				'soutez_id' => $soutez_id,
				'tym_id' => $id
			);
			$this->connection->insert($this->tym, $data)->execute(dibi::IDENTIFIER);
		}
		
		return true;
		
	}
	
	public function tym_soutez_update($id, array $data)
	{
		return $this->connection->update($this->tym, $data)->where('id=%i', $id)->execute();
	}

	public function tym_soutez_insert(array $data)
	{
		return $this->connection->insert($this->tym, $data)->execute(dibi::IDENTIFIER);
	}
	public function tym_soutez_findAll()
	{
		return $this->connection->select('*')->from($this->tym);
	}
	public function tym_soutez_findById($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->tym." WHERE id=%i",$id);
	}
	public function tym_soutez_findBySoutezId($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->tym." WHERE soutez_id=%i",$id);
	}
	public function tym_soutez_findByTymSoutezId($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->tym." WHERE tym_id=%i",$id);
	}
	public function tym_soutez_delete($id)
	{
		return $this->connection->query("DELETE FROM ".$this->tym." WHERE id=%i", $id);
	}
	
	
	//RELACE s tabulkou ROZHODCI_SOUTEZ
	public function rozhodci_soutez_findAll()
	{
		return $this->connection->select('*')->from($this->rozhodci)->orderBy('soutez_id');
	}
	public function rozhodci_soutez_findById($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->rozhodci." WHERE id=%i",$id);
	}
	public function rozhodci_soutez_delete($id)
	{
		return $this->connection->query("DELETE FROM ".$this->rozhodci." WHERE id=%i", $id);
	}
	public function rozhodci_soutez_update($id, array $data)
	{
		return $this->connection->update($this->rozhodci, $data)->where('id=%i', $id)->execute();
	}
	public function rozhodci_soutez_insert(array $data)
	{
		return $this->connection->insert($this->rozhodci, $data)->execute(dibi::IDENTIFIER);
	}
	

	//RELACE s tabulkou POZICE_SOUTEZ
	public function pozice_soutez_findAll()
	{
		return $this->connection->select('*')->from($this->pozice);
	}
	public function pozice_soutez_findById($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->pozice." WHERE id=%i",$id);
	}
	public function pozice_soutez_delete($id)
	{
		return $this->connection->query("DELETE FROM ".$this->pozice." WHERE id=%i", $id);
	}
	public function pozice_soutez_update($id, array $data)
	{
		return $this->connection->update($this->pozice, $data)->where('id=%i', $id)->execute();
	}
	public function pozice_soutez_insert(array $data)
	{
		return $this->connection->insert($this->pozice, $data)->execute(dibi::IDENTIFIER);
	}	
}
